package com.healthcloud.controller.modules.hospital;

import java.util.List;
import java.util.Map;

import com.healthcloud.annotation.SysLog;
import com.healthcloud.entity.MsgVo;
import com.healthcloud.utils.ApiRes;
import com.healthcloud.utils.PageParam;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.healthcloud.entity.SysOrganiEntity;
import com.healthcloud.service.hospital.SysOrganiService;
import springfox.documentation.annotations.ApiIgnore;


/**
 * 
 * 
 * @author yucl
 * @email yucailong1111@126.com
 * @date 2018-06-11 17:34:26
 */
@ApiIgnore
@Controller
@RequestMapping("organi")
public class SysOrganiController {
	@Autowired
	private SysOrganiService sysOrganiService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("organi:list")
	public String list(@RequestParam Map<String, Object> params){
		return "bus/sysorgani/sysorganimgr";
	}
	
	@RequiresPermissions("organi:list")
	@RequestMapping(value = "/searchSysOrgani")
	@ResponseBody
    public MsgVo  searchSysOrgani(SysOrganiEntity bean,Model model, @ModelAttribute("pageParam")PageParam pageParam){
        List<SysOrganiEntity> list = sysOrganiService.queryList(bean, pageParam);
        int count = sysOrganiService.queryTotal(bean);
        return new MsgVo(list,count,count,pageParam.getsEcho());
    }
	
	/**
	 * 新增页
	 */
	@RequestMapping("/tosave")
	@RequiresPermissions("organi:save")
    public String toSave(Model model) {

        List<SysOrganiEntity> organis = sysOrganiService.queryListAll();
        for(SysOrganiEntity organi : organis){
            organi.setOpen(true);
        }

        model.addAttribute("organis", organis);
        return "bus/sysorgani/addOrUpdatesysorgani";
    }
    
	/**
	 * 保存
	 */
    @SysLog("保存SysOrgani")
    @RequestMapping("/save")
    @ResponseBody
    public ApiRes save(SysOrganiEntity entity) {
        sysOrganiService.save(entity);
        return ApiRes.SUCCESS;
    }
	
	/**
	 * 修改页
	 */	
	@RequestMapping("/toupdate")
    @RequiresPermissions("organi:update")
    public String toUpdate(@Param("id") Long id, Model model) {

        SysOrganiEntity entity = sysOrganiService.queryObject(id);
        model.addAttribute("entity", entity);

        List<SysOrganiEntity> organis = sysOrganiService.queryListAll();
        model.addAttribute("organis", organis);
        for(SysOrganiEntity organi : organis){
            organi.setOpen(true);
            if(organi.getId().equals(entity.getParentId())){
                organi.setChecked(true);
            }
        }

        return "bus/sysorgani/addOrUpdatesysorgani";
    }
	
    /**
     * 修改
     */
    @SysLog("修改SysOrgani")
    @RequestMapping("/update")
    @ResponseBody
    public ApiRes update(SysOrganiEntity entity) {
        sysOrganiService.update(entity);
        return ApiRes.SUCCESS;
    }

	
    /**
     * 删除
     */
    @SysLog("删除SysOrgani")
    @RequestMapping("/delete")
    @RequiresPermissions("organi:delete")
    @ResponseBody
    public ApiRes delete(Integer[] ids) {
        sysOrganiService.deleteBatch(ids);
        return ApiRes.SUCCESS;
    }

}
